package com.mySampleApplication.server;
import com.mySampleApplication.client.CodeRuleAtter.CodeRuleAtterDto;
import com.mySampleApplication.client.codeRule.CodeRuleDto;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;


public class Encoder {
    private String prefix;
    private String suffix;
    private String delimiter;
    private List<String> segments;

    public Encoder(String prefix, String suffix, String delimiter) {
        this.prefix = prefix;
        this.suffix = suffix;
        this.delimiter = delimiter;
        this.segments = new ArrayList<>();
    }

    public void addSegment(String segment) {
        segments.add(segment);
    }

    public String encode() {
        StringBuilder encodedString = new StringBuilder();
        encodedString.append(prefix);
        //encodedString.append(delimiter);
        for (int i = 0; i < segments.size(); i++) {
            encodedString.append(segments.get(i));

            if (i != segments.size() - 1) {
                encodedString.append(delimiter);
            }
        }
        //encodedString.append(delimiter);
        encodedString.append(suffix);

        return encodedString.toString();
    }


    public static void main(String[] args) {
        String serialNumber1 = SerialNumberGenerators.getNextSerialNumber(8, "", 5);
        System.out.println(serialNumber1);
        String serialNumber2 = SerialNumberGenerators.getNextSerialNumber(8, "", 5);
        System.out.println(serialNumber2);
        String serialNumber3 = SerialNumberGenerators.getNextSerialNumber(8, "", 5);
        System.out.println(serialNumber3);
        String serialNumber4 = SerialNumberGenerators.getNextSerialNumber(8, "", 5);
        System.out.println(serialNumber4);
        Map<String , Object> map = new HashMap<>();
        map.put("cglx","dl");
        map.put("mrp_lx","xdl");
        CodeRuleDto data = CodeRuleServlet.getSelectMap("233e4f3565c10c6b79b6f83ebea2e176");
        Encoder encoder = new Encoder(data.getMap().get("qz"), data.getMap().get("hz"), data.getMap().get("fgf"));
        CodeRuleAtterDto dataAtter = CodeRuleAtterServlet.getSelectById(data.getMap().get("id"));
        List<Map<String, String>> list = dataAtter.getKeyValueMap();
        list.forEach(a->{
            //流水号
            if(a.get("fdlx").equals("1")){
                SerialNumberGenerator generator = new SerialNumberGenerator(6, 1, "%06d");
                encoder.addSegment(generator.getNextSerialNumber());
            }//日期值
            else if(a.get("fdlx").equals("2")){
                LocalDate currentDate = LocalDate.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
                String formattedDate = currentDate.format(formatter);
                encoder.addSegment(formattedDate);
            }//固定值
            else if(a.get("fdlx").equals("3")){
                encoder.addSegment(a.get("gdz"));
            }//字段值
            else if(a.get("fdlx").equals("4")){
                encoder.addSegment(map.get(a.get("zdsxm")).toString());
            }
        });
        String encoded = encoder.encode();
        System.out.println(encoded);  // 输出：[segment1-segment2-segment3]
    }
}
